Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
Why does "info()" not work even though the "read" field changes?

I have a factory function, where I am returning a Book Object. When I create an object of the Book and change a field value, this is not reflected in the method of the Object. Any idea why this may be happening?

const Book = (title, author, pages, read) => {
    const info = () => {
        if (read == true) {
            return `${title} by ${author} - ${pages} pages - already read`;
        } else {
            return `${title} by ${author} - ${pages} pages - not read yet`;
        }
    }
    return { title, author, pages, read, info }; 
}

I create a Book Object using let book = Book(title, author, pages, read); And change the value of read by accessing the read field directly. However, when I change the value of read, this is not reflected in the info method.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

The object properties are not aliases for the variables, the variable values are used when creating the object.

To refer to the object properties, you need to use this.

And in order to have this refer to the object, you must use a traditional function rather than an arrow function.

const Book = (title, author, pages, read) => {
    const info = function() {
        if (this.read) {
            return `${this.title} by ${this.author} - ${this.pages} pages - already read`;
        } else {
            return `${this.title} by ${this.author} - ${this.pages} pages - not read yet`;
        }
    }
    return { title, author, pages, read, info }; 
}

let b = Book("Title", "Author", 10, false);
console.log(b.info());

b.author = "New Author";
console.log(b.info());

about 4 years ago · Juan Pablo Isaza Relatório

0

In modern JS, you'd most probably want to use a class here which simplifies working with objects by alot:

class Book {
  constructor(title, author, pages, read) {
    this.title = title;
    this.author = author;
    this.pages = pages;
    this.read = read;
  }
   
  get info() {
    return `"${this.title}" by ${this.author} - ${this.pages} pages - ${this.read ? 'already read' : 'not read yet'}`
  }
}

let b = new Book("No Place To Hide", "Glenn Greenwald", 259, false);
console.log(b.info);

b.author = "G. Greenwald";
console.log(b.info);

b.read = true;
console.log(b.info);

// you can even test if b is a Book:
// (which you could not with a factory function)
console.log(b instanceof Book);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda